package com.example.ulit;

import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
 * @author lqm
 * @ClassName EmailUtil
 * @description: 发送邮件工具类
 */
@Component
public class EmailUtil {
    // 发件人电子邮箱
    final static String from = "3059812387@qq.com";
    // 发件人电子邮箱授权码  设置pop3/smtp服务时获取的授权码
    final static String pwd = "zoektxgbzywjdfhe";
    // 指定发送邮件的主机 可通过文档中1.2.1获取
    final static String host = "smtp.qq.com";

    /**
     * 发送简答邮件
     *
     * @param toEmail    收件人电子邮箱
     * @param titleStr   邮件标题
     * @param contentStr 邮件内容
     */
    public static void sendString(String toEmail, String titleStr, String
            contentStr) {
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.auth", "true");
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new
                Authenticator() {
                    public PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(from, pwd); //发件人邮件用户名、授权码
                    }
                });
        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);
            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));
            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            // Set Subject: 头部头字段
            message.setSubject(titleStr);
            // 设置消息体
            message.setText(contentStr);
            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }

    /**
     * 发送Html邮件
     *
     * @param toEmail  收件人电子邮箱
     * @param titleStr 邮件标题
     * @param htmlStr  带html标签的邮件内容
     */
    public static void sendHtml(String toEmail, String titleStr, String htmlStr) {
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.auth", "true");
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new
                Authenticator() {
                    public PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(from, pwd); //发件人邮件用户名、授权码
                    }
                });
        try {
            // 创建默认的 MimeMessage 对象。
            MimeMessage message = new MimeMessage(session);
            // Set From: 设置发件人
            message.setFrom(new InternetAddress(from));
            // Set To: 设置收件人
            message.addRecipient(Message.RecipientType.TO, new
                    InternetAddress(toEmail));
            // Set Subject: 头字段
            message.setSubject(titleStr);
            // 发送 HTML 消息, 可以插入html标签3.编写测试类

            message.setContent(htmlStr, "text/html");
            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}